package net.ycxinxing.mcpebox.ui;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import cn.bmob.v3.BmobACL;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import java.io.File;
import net.ycxinxing.mcpebox.Pro.R;
import net.ycxinxing.mcpebox.baseActivity;
import net.ycxinxing.mcpebox.bean.mUser;
import net.ycxinxing.mcpebox.mDialog;
import net.ycxinxing.mcpebox.utils.Utils;
import net.ycxinxing.mcpebox.widget.mTextView;

public class RegisterActivity extends baseActivity
{
	private String Imgpath;
	private ImageView IVAvatar;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		LinearLayoutCompat l=new LinearLayoutCompat(this);
		l.setOrientation(1);
		TextInputLayout LUserName=new TextInputLayout(this);
		final TextInputEditText UserName=new TextInputEditText(this);
		LUserName.addView(UserName);
		LUserName.setHint("用户名");
		UserName.setMaxLines(1);
		UserName.setSingleLine(true);
		TextInputLayout Lemail=new TextInputLayout(this);
		final TextInputEditText email=new TextInputEditText(this);
		Lemail.addView(email);
		Lemail.setHint("邮箱");
		email.setMaxLines(1);
		email.setSingleLine(true);
		email.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
		TextInputLayout LPassWord=new TextInputLayout(this);
		final TextInputEditText Password=new TextInputEditText(this);
		LPassWord.addView(Password);
		LPassWord.setHint("密码");
		Password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		TextInputLayout LPassWord2=new TextInputLayout(this);
		final TextInputEditText Password2=new TextInputEditText(this);
		LPassWord2.addView(Password2);
		LPassWord2.setHint("再次输入密码");
		Password2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		RadioGroup rg=new RadioGroup(this);
		final RadioButton man=new RadioButton(this);
		man.setText("男");
		man.setChecked(true);
		final RadioButton woman=new RadioButton(this);
		woman.setText("女");
		man.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener(){

				@Override
				public void onCheckedChanged(CompoundButton p1, boolean p2)
				{
					if (p2)
					{
						woman.setChecked(false);
					}
					// TODO: Implement this method
				}
			});
		woman.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener(){

				@Override
				public void onCheckedChanged(CompoundButton p1, boolean p2)
				{
					if (p2)
					{
						man.setChecked(false);
					}
					// TODO: Implement this method
				}
			});
		rg.setOrientation(0);
		rg.addView(man);
		rg.addView(woman);
		TextInputLayout LUserSign=new TextInputLayout(this);
		final TextInputEditText UserSign=new TextInputEditText(this);
		LUserSign.addView(UserSign);
		LUserSign.setHint("个性签名");
		UserSign.setMaxLines(1);
		UserSign.setSingleLine(true);
		IVAvatar=new ImageView(this);
		
		IVAvatar.setImageResource(R.drawable.user);
		IVAvatar.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
					startActivityForResult(i, 12345);
					// TODO: Implement this method
				}
			});
		Button register=new Button(this);
		register.setText("注册");
		l.addView(LUserName);
		l.addView(Lemail);
		l.addView(LPassWord);
		l.addView(LPassWord2);
		l.addView(LUserSign);
		l.addView(rg);
		l.addView(new mTextView(this, "头像"));
		l.addView(IVAvatar);
		l.addView(register);
		IVAvatar.getLayoutParams().width=Utils.dip2px(this,80);
		IVAvatar.getLayoutParams().height=Utils.dip2px(this,80);
		register.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					if (Password.getText().toString().equals(Password2.getText().toString()))
					{
						final ProgressDialog pd=new ProgressDialog(RegisterActivity.this);
						pd.setMessage("请稍后...");
						pd.setCancelable(false);
						pd.show();
						final mUser user=new mUser();
						user.setEmail(email.getText().toString());
						user.setUsername(UserName.getText().toString());
						user.setPassword(Password2.getText().toString());
						user.setSign(UserSign.getText().toString());
						user.setIsMan(man.isChecked());
						BmobACL acl=new BmobACL();
						acl.setPublicWriteAccess(false);
						user.setACL(acl);
						user.signUp(new SaveListener<mUser>(){
								@Override
								public void done(mUser p1, BmobException p2)
								{
									if (p2 != null)
									{
										mDialog dlg=new mDialog(RegisterActivity.this){
											@Override
											public void onButtonClick(int i)
											{
												// TODO: Implement this method
											}
										};
										dlg.setTitle("注册发生了错误");
										dlg.setMsg("错误码:" + p2.getErrorCode() + "\n错误详情:\n" + p2.getMessage());
										dlg.setButton3("确定");
										pd.dismiss();
										dlg.show();
									}
									else
									{
										if(Imgpath!=null)
										{
										final BmobFile file=new BmobFile(new File(Imgpath));
										file.uploadblock(new UploadFileListener(){
												@Override
												public void done(BmobException p1)
												{
													user.setAvatar(file);
													user.update(new UpdateListener(){
															@Override
															public void done(BmobException p1)
															{
																if (p1 != null)
																{
																	mDialog dlg=new mDialog(RegisterActivity.this){
																		@Override
																		public void onButtonClick(int i)
																		{
																			// TODO: Implement this method
																		}
																	};
																	dlg.setTitle("注册发生了错误");
																	dlg.setMsg("错误码:" + p1.getErrorCode() + "\n错误详情:\n" + p1.getMessage());
																	dlg.setButton3("确定");
																	pd.dismiss();
																	dlg.show();
																}
																else
																{
																	mDialog dlg=new mDialog(RegisterActivity.this){
																		@Override
																		public void onButtonClick(int i)
																		{
																			RegisterActivity.this.finish();
																			// TODO: Implement this method
																		}
																	};
																	dlg.setMsg("注册成功");
																	dlg.setButton3("返回主页");
																	dlg.setCanClose(false);
																	pd.dismiss();
																	dlg.show();
																}
																// TODO: Implement this method
															}
														});
												}
											});
										}else{
											mDialog dlg=new mDialog(RegisterActivity.this){
												@Override
												public void onButtonClick(int i)
												{
													RegisterActivity.this.finish();
													// TODO: Implement this method
												}
											};
											dlg.setMsg("注册成功");
											dlg.setButton3("返回主页");
											dlg.setCanClose(false);
											pd.dismiss();
											dlg.show();
										}
									}
									// TODO: Implement this method
								}
							});
						// TODO: Implement this method

					}
					else
					{
						mDialog dlg=new mDialog(RegisterActivity.this){
							@Override
							public void onButtonClick(int i)
							{
								// TODO: Implement this method
							}
						};
						dlg.setMsg("两次密码不一致");
						dlg.setButton3("确定");
						dlg.show();

					}
					// TODO: Implement this method
				}
			});
		setContentView(l);
		// TODO: Implement this method
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 12345 && resultCode == RESULT_OK && null != data)
		{  
			Uri selectedImage = data.getData();  
			String[] filePathColumn = { MediaStore.Images.Media.DATA };  
			Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);  
			cursor.moveToFirst();  
			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);  
			Imgpath = cursor.getString(columnIndex);  
			cursor.close();
			String imageUrl=ImageDownloader.Scheme.FILE.wrap(Imgpath);
			ImageLoader.getInstance().displayImage(imageUrl,IVAvatar);
		}
	}

}
